<template>
  <div class="websocket">
    <el-row>
      <el-col :span="8" :offset="8">
        <div
          class="msg-box"
          ref="msg-box"
          v-for="(item, index) in list"
          :key="index"
        >
          <!--发送来的消息-->
          <div>
            <el-avatar
              :size="30"
              style="float:right"
              src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"
            />
            <el-tag type="success" style="float:right">
              {{ item.content }}
            </el-tag>
          </div>

          <br />
          <!--发出去的消息-->
          <div>
            <el-avatar
              :size="30"
              style="float:left"
              src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
            />
            <el-tag type="success" style="float:left">{{
              item.content
            }}</el-tag>
          </div>
          <br />
        </div>
        <el-form :model="message" ref="message" :rules="rules">
          <el-form-item prop="content">
            <el-input type="textarea" v-model="message.content"> </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('message')">发送</el-button>
            <el-button @click="resetForm('message')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  props: {},
  data() {
    return {
      // 消息对象
      message: {},
      // 聊天记录的数组
      list: [],
      ws: null,
      count: 0,
      // 当前用户ID
      userId: "",
      // 当前用户昵称
      username: "",
      // 当前用户头像
      avatar: "",
      rules: {
        content: [
          { required: true, message: "不能发送空内容", trigger: "blur" }
        ]
      }
    };
  },
  mounted() {},
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log("触发这个");
          this.list.push(this.message);
        } else {
          console.log("error submit!!");
          return false;
        }
        this.message.content = null;
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>
<style scoped>
.websocket {
  line-height: 50px;
}
</style>
